package se137.hrbust.hlgshopping137.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
//import com.second.hand.trading.server.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PayUtil {
//    @Autowired
//    private OrderService orderService;
    //appid
    private final String APP_ID = "9021000153640891";
    //应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCVcSXjtFE9UvyWcVq/K7KNKV4Ps1azh+qKGEH5amo5NX0HTZXXt5dR9zE4uRqGaAmdl0xDVsdt+gqvRFBbgJlC/fRgEePP9trRBd4/HhC7Ai7LIFOU6DEn2/JHh9jTrs0jODeDhJNsdXYNqa06NzijXSovteWUZdIU3xYUvt0xIBUsjCbbSzGuDZNsAqeDbIAPgTQtmjCicNndpH1jvwJ4zU8uOrjwvXtIgJVn+4fQL3NMhpPyXb1oDyyRWCwtJKlHpm7yQ+Ueh5nr3IhPqcsQng4Z7Bnk6dLXdgU1B7u/yYCU0CwHy2v276Rqh/bZP3f62WmzyCIFgrUTI6lwzrwbAgMBAAECggEBAIcLW5j8fplzH6x3vPCadVyY8FYx0vyjqy+loiDYqCsQie6KjwkD7+ty/PjsVOr2oxHVAmarycY5OWSO0CsP9++Q/D/AiuSIh85xhhf1fFryKwUGLmGWvazuVm+LdEY+ZUQZbhkcOfi94LOSDGdaNYVzEEdTEabbqE+SFSskG1Bi8ka/mjXG4A6ur3MhxSdU2utvpxytGz3kVB9qfPJBwME0dlyvt4GUxxYMy/IObRRhBpNTSJJrvvNXvCG7SbeTRc3APisDlndB2I4hiy8/m9hbJ764nRMCDN0A6baM/aXw7O43daTzSm5MG6Byl7ywcKRW9R0dR2rbt3+mNL1zK0ECgYEA0Y4iRrOBNu8qyR0zMBxKPjYQ0Fgk0Taaz5gup5VkDV8H+hFyaMClDKHjXUOIispNGlgr2neU/6IWQS6ZwGuGfUyfZWAR2ApVKJCE8Ng4HT9NZdgnd0UZATF7UnF0TNVwZRu1cMgDADh/sV8Lt3qCmCYR1FtAcPHeVDXiflnzNuECgYEAtpBGVKdEikbTVy8DGEhaK2eqHvAmXjyhXl6W6jFZeGshdn7eBU4vI/k7vUayJVzdLYPFjyP+Igy2uqVYZ5F6AuBemf+Ilp8ug6rpuL0m5HpGN7aK9YaNukwVC8luF+jWRSJUeDhvRxOhRoInajKXAJFIg1DVg6mUw3DL3golHnsCgYALcNEaPw6c6y3Q0RFMXR8IincciNNpwKtvDgXuDE/sEvYihvVSFsJsECKTou4/dZAEEjBCuyidVEIWyO6H/NbY3GgPZtFSlC3WCx3W8FcP8bbOCqEM9+7Hi+YJuDv7PePnAT5ErAbN6ke+bUv8dmnO2LU8fMbl466Ztl9pOov2wQKBgQCrpj0qTNbpuCvvVhlv1F9LXj2atyssAY+rvf3hFiNMUFX3JtOzY055YVXk4bqP3J9ssu+mZS4Fb7TDCsFzJ1nzqGfqHVOcYvCGBcWY1+fgfILW0UAYF7ncBrrnCS4FNvt3pw6Ktm0SBAfn1moetJA7jV4DcZY/I8vmcoCMNTevTwKBgHgrD2eTkWHW+wil5aFvxSmC7bojL679TUkz1rKGIhs8xCKf4rZzJYeZdfPeR+fcTrLU11HfFa6I5d0Vx0PbLdFw8veXbRTbhN/h5rzMuJKh9C0qjAgo8UP9gSP67cmNwJXRgT4yi4qmhrwaGlsBF6FpGQT5/W5aZU1jAxrENyqa";

    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2TOCM1/k424qJP2NycEBFIPxmfh60rxA5/jqBG4FT28/TZrt+jBTQdPfp7z2zzrBJcFFjreamAc2iVskESgHXhy/mTQpqu8bZH7xYuCKQAqiRanrraeC03j8J14QNVLUbZlZLa8U8qzrRXaLYUXyQ4lfVteB3DT5qe/yvd92uQ2rFjByg77XcXRAFKxQMB6KPAvBfp2Y8pzZSFH+mDiFgpybSIHzQVSUlGi43mkzyw0qaegfjqIRZvJ2fDzGtKrDsLhnFzsUM/kb7e9eEJMMC24dJZNrrWuFnYkV1AhSMwFYkY1xlWDGBqVhXBviM4ETQvWmIzo4KjviPDsZjOh8pQIDAQAB";
    //这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private final String FORMAT = "JSON";
    //签名方式
    private final String SIGN_TYPE = "RSA2";
    //支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
    private final String NOTIFY_URL = "http://v7c82e66.natappfree.cc//api/alipay/toSuccess";
    //支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
    private final String RETURN_URL = "http://localhost:8080/api/alipay/toSuccess";
    private AlipayClient alipayClient = null;
    //支付宝官方提供的接口
    public String sendRequestToAlipay(String outTradeNo, Float totalAmount, String subject) throws AlipayApiException {
        //获得初始化的AlipayClient
        alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(RETURN_URL);
        alipayRequest.setNotifyUrl(NOTIFY_URL);

        //商品描述（可空）
        String body = "";
        alipayRequest.setBizContent("{\"out_trade_nos\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        System.out.println("返回的结果是："+result );
        return result;
    }

    //    通过订单编号查询
    public String query(String id){
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", id);
        request.setBizContent(bizContent.toString());
        AlipayTradeQueryResponse response = null;
        String body=null;
        try {
            response = alipayClient.execute(request);
            body = response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        return body;
    }
}
